<template>
  <div>
    <Content title="文件夹" :subTitle="isNew?'新建文件夹':'编辑文件夹'" path="/fileMapping" v-loading="loading">
      <div slot="body">
        <div style="padding-left: 20px">
          <p style="font-size: 20px">{{form.parentName?form.parentName+'目录':'根目录'}}</p>
          <el-form :model="form" label-position="left" :rules="rules" ref="form" label-width="140px"
                   class="fileMapping-form">
            <el-form-item label="文件夹名称" prop="name" class="my-form-item">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="文件夹编码" prop="key" class="my-form-item">
              <el-input v-model="form.key"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </Content>
    <div style="padding: 15px">
      <router-link to="/fileMapping">
        <el-button plain round type="info" icon="el-icon-back">返回</el-button>
      </router-link>
      <el-button plain type="primary" @click="submitForm('form')" icon="el-icon-circle-check">保存</el-button>
    </div>
  </div>
</template>
<script>
  import util from '../../utils/util'
  import {FileMapping} from '../../utils/resource'
  import Content from '../../components/Content.vue'
  import {mapState, mapActions} from 'vuex'
  import {showSaveResponse, validFieldExists, validateForm} from '../../utils/element-helper'

  export default {
    name: 'fileMappingFolder',
    components: {
      Content,
    },
    data() {
      return {
        loading: false,
        isNew: true,
        initForm: {},// for edit
        form: {
          key: "",
          ext: "folder",
          size: 0,
          name: "",
          folder: 1,
          parent: 0,
          ancestors: "0",
        },
        rules: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'},
          ],
          key: [
            {required: true, message: '编码不能为空', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                this.validateExists(rule, value, callback)
              }, trigger: 'blur'
            }
          ],
        },
      }
    },
    created() {

      let id = this.$route.params.id
      this.isNew = (id === undefined || id ==="new")
      if (!this.isNew) {
        //编辑
        this.loadFileMapping()
      } else {
        let parent = this.$route.params.parent
        //新建根目录的
        if (!parent) {
        }else{
          //新建非根目录的
          this.form.parent = parent
          this.form.parentName = this.$route.params.parentName
        }
      }
    },
    computed: {},
    methods: {
      async loadFileMapping() {
        let res = await FileMapping.get(this.$route.params.id)
        this.form = res.data
        this.initForm = res.data
      },
      submitForm(formName) {
        return validateForm(this, formName, () => {
          showSaveResponse(this, () => FileMapping.save(this.form, 'post'), (res) => this.$router.back())
        })
      },
      validateExists(rule, value, callback) {
        validFieldExists(rule, value, callback, this.initForm[rule.field] !== value || this.isNew, (rule, value) => FileMapping.exists(rule, value))
      },
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .fileMapping-form {
    width: 90%;
  }

  .my-form-item {
    max-width: 700px;
  }

</style>
<style>
  .my-form-input {
    max-width: 330px;
  }

</style>
